import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import routerBeforeEach from '@/router/nav-guard'
import routes from '~pages' // 导入插件自动生成的路由
import { setupLayouts } from 'virtual:generated-layouts'
const route = setupLayouts(routes)

/**
 * constantRoutes
 * 不需要权限的基础路由
 * 所有角色都可以访问
 * */
export const constantRoutes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Home',
    meta: { title: '主页' },
    component: () => import('@/views/home/index.vue'),
  },
  // {
  //   path: '/login',
  //   name: 'Login',
  //   meta: { title: '登录', hidden: true },
  //   component: () => import('@/views/login/index.vue'),
  // },
  // {
  //   path: '/404',
  //   name: '404',
  //   component: () => import('@/views/error-page/404.vue'),
  // },
  // // 404铺底路由
  // {
  //   path: '/:pathMatch(.*)*',
  //   name: 'NotFound',
  //   redirect: '/404',
  //   meta: { hidden: true },
  // },
  ...route, //使用
]
/**
 * asyncRoutes
 * 需要根据用户角色动态加载的路由
 * */

const router = createRouter({
  // history: createWebHistory(import.meta.env.VITE_BASE_BUILD_DIR as string),
  // history: createWebHashHistory(),
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: constantRoutes,
})
router.beforeEach((to, form, next) => {
  routerBeforeEach(to, form, next, router)
})
// router.beforeEach((to, from) => {

//   return true
// })

export default router
